package com.example.doctor.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;

public interface CommentMapper {
    @SelectProvider(type = SqlBuilder.class, method = "CommentListSQL")
    @Results(id = "CommentListResult", value = { @Result(property = "OrderNO", column = "order_no_nvarchar", id = true),
            @Result(property = "CreateTime", column = "create_time_datetime"),
            @Result(property = "Header", column = "header_nvarchar"),
            @Result(property = "Name", column = "name_nvarchar"),
            @Result(property = "DScore", column = "d_score_decimal"),
            @Result(property = "DRemark", column = "d_remark_nvarchar"),
            @Result(property = "Re", column = "re_nvarchar"),
            @Result(property = "ReTime", column = "re_time_datetime") })
    List<Map<String, Object>> CommentList(@Param("UID") long UID, @Param("IsComment") Boolean IsComment, @Param("search") String search,
            @Param("skip") int skip, @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "countCommentListSQL")
    long countCommentList(@Param("UID") long UID, @Param("IsComment") Boolean IsComment, @Param("search") String search);

    @SelectProvider(type = SqlBuilder.class, method = "CommentDetailSQL")
    @Results(id = "CommentDetailResult", value = { 
            @Result(property = "OrderNO", column = "order_no_nvarchar", id = true),
            @Result(property = "CreateTime", column = "create_time_datetime"),
            @Result(property = "Header", column = "header_nvarchar"),
            @Result(property = "Name", column = "name_nvarchar"),
            @Result(property = "Age", column = "age_int"),
            @Result(property = "Sex", column = "sex_nvarchar"),
            @Result(property = "DScore", column = "d_score_decimal"),
            @Result(property = "DRemark", column = "d_remark_nvarchar"),
            @Result(property = "IsComment", column = "is_comment_bit"),
            @Result(property = "Re", column = "re_nvarchar"),
            @Result(property = "ReTime", column = "re_time_datetime") })
    Map<String, Object> CommentDetail(@Param("UID") long UID, @Param("OrderNO") String OrderNO);

    class SqlBuilder {
        public String CommentListSQL(@Param("UID") long UID, @Param("IsComment") Boolean IsComment, @Param("search") String search, @Param("skip") int skip,
                @Param("limit") int limit) {
            String sql = "select a.order_no_nvarchar,a.create_time_datetime,c.header_nvarchar,"
                    + " b.name_nvarchar,a.d_score_decimal,a.d_remark_nvarchar,a.re_nvarchar,a.re_time_datetime"
                    + " from order_comment a"
                    + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join client_patients_info c on a.pid_bigint = c.pid_bigint"
                    + " where a.dc_id_bigint = #{UID}"; 
                    if(IsComment != null)
                        sql +=" and a.is_comment_bit = #{IsComment}";
                    if(search != null && search.length() > 0)
                        sql += " and c.name_nvarchar LIKE CONCAT ('%',#{search},'%') ";
                    sql += " order by a.create_time_datetime desc" + " limit #{skip},#{limit}";
            return sql;
        }

        public String countCommentListSQL(@Param("UID") long UID, @Param("IsComment") Boolean IsComment, @Param("search") String search) {
            String sql = "select count(*)"
                    + " from order_comment a"
                    + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join client_patients_info c on a.pid_bigint = c.pid_bigint"
                    + " where a.dc_id_bigint = #{UID}";
            if(IsComment != null)
                sql +=" and a.is_comment_bit = #{IsComment}";
            if(search != null && search.length() > 0)
                sql += " and c.name_nvarchar LIKE CONCAT ('%',#{search},'%') ";
            sql += " order by a.create_time_datetime desc";
            return sql;
        }

        public String CommentDetailSQL(@Param("UID") long UID, @Param("OrderNO") String OrderNO) {
            String sql = "select a.order_no_nvarchar,a.create_time_datetime,c.header_nvarchar,b.name_nvarchar,b.age_int,b.sex_nvarchar,"
            + " a.d_score_decimal,a.d_remark_nvarchar,a.is_comment_bit,a.re_nvarchar,a.re_time_datetime"
            + " from order_comment a"
            + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join client_patients_info c on a.pid_bigint = c.pid_bigint"
            + " where a.dc_id_bigint = #{UID} and a.order_no_nvarchar = #{OrderNO}";
            return sql;
        }
    }
}
